<?php
	class search
	{
		var $nRating 		= '';
		var $nOrder 		= '';
		var $nAkio 			= '';
		var $intervalDate 	= '';
		var $nameRater 		= '';
		var $nameConsultant = '';
		var $minMark 		= 0;
		var $maxMark 		= 100;
		var $improvement 	= '';
		var $idTopic 		= '';
		var $idCountry 		= 0;
		var $idRecipient 	= 0;
		var $groups 		= array();
		var $evalType 		= '';
		var $querySearch 	= '';
		var $team 			= '';
		var $idForm 		= 0;

		/**
			Check if the data are valid then add it in the class variables 
		*/
		function validateData($data)
		{
			if (isset($data['n_rating']) && $data['n_rating'] != '')
			{
				$this->nRating = $data['n_rating'];
			}
			if (isset($data['n_order']) && $data['n_order'] != '')
			{
				$this->nOrder = $data['n_order'];
			}
			if (isset($data['n_akio']) && $data['n_akio'] != '')
			{
				$this->nAkio = $data['n_akio'];
			}
			if (isset($data['ratingDate']) && $data['ratingDate'] != '')
			{
				$this->intervalDate = str_replace(' ', '', $data['ratingDate']);
			}
			if (isset($data['name_rater']) && $data['name_rater'] != '')
			{
				$this->nameRater = $data['name_rater'];
			}
			if (isset($data['name_consultant']) && $data['name_consultant'] != '')
			{
				$this->nameConsultant = $data['name_consultant'];
			}
			if (isset($data['min_mark']) && $data['min_mark'] != '')
			{
				$this->minMark = intval($data['min_mark']);
			}
			if (isset($data['max_mark']) && $data['max_mark'] != '')
			{
				$this->maxMark = intval($data['max_mark']);
			}
			if (isset($data['improvement']) && $data['improvement'] != '')
			{
				$this->improvement = $data['improvement'];
			}
			if (isset($data['topic']) && $data['topic'] != '')
			{
				$this->idTopic = intval($data['topic']);
			}
			if (isset($data['country']) && $data['country'] != '')
			{
				$this->idCountry = intval($data['country']);
			}
			if (isset($data['recipient']) && $data['recipient'] != '')
			{
				$this->idRecipient = intval($data['recipient']);
			}
			if (isset($data['eval_type']) && $data['eval_type'] != '')
			{
				$this->evalType = $data['eval_type'];
			}
			if (isset($data['team']) && $data['team'] != '')
			{
				$this->team = $data['team'];
			}

			if (isset($data['id_form']) && $data['id_form'] != '')
			{
				$this->idForm = $data['id_form'];
			}

			foreach ($data as $key => $value)
			{
				if (strpos($key, 'group') !== false)
				{
					$this->groups[] = $value;
				}
			}
		}

		/**
			Generate the good query based on the data stored in class variable
		*/
		function generateQuery()
		{
			$this->querySearch = "SELECT 
							`rating`.`id`, 
							`rating`.`n_order`, 
							`rating`.`n_akio`, 
							`rating`.`date_eval`, 
							`rating`.`name_rater`, 
							`rating`.`name_consultant`, 
							`rating`.`final_mark`,
							`rating`.`improvement`,
							`rating`.`team`,
							`topic`.`description` AS `topic`, 
							`country`.`name` AS `country_name`, 
							`rating`.`groups`, 
							`rating`.`eval_type`,
							`recipient`.`name` AS `recipient`,
							`evaluation_form`.`name`
						FROM 
							`rating` 
						LEFT JOIN 
							`topic` ON `topic`.`id`=`rating`.`id_topic` 
						LEFT JOIN 
							`country` ON `rating`.`id_country`=`country`.`id` 
						LEFT JOIN 
							`recipient` ON `recipient`.id=`rating`.`id_recipient`
						LEFT JOIN
							`rating_criteria` ON `rating_criteria`.`id_rating` = `rating`.`id`
						LEFT JOIN
							`criteria` ON `criteria`.`id`=`rating_criteria`.`id_criteria`
						LEFT JOIN
							`evaluation_form` ON `criteria`.`id_evaluation_form`=`evaluation_form`.`id`
						WHERE ";
			$and = 0;
			if ($this->team != '')
			{
				if ($and > 0)
				{
					$this->querySearch .= " AND ";
				}
				$this->querySearch .= '`rating`.`team` LIKE "%' . $this->team .'%"';
				$and++;
			}

			if ($this->idForm != 0)
			{
				if ($and > 0)
				{
					$this->querySearch .= " AND ";
				}
				$this->querySearch .= '`evaluation_form`.`id` = ' . $this->idForm . ' ';
				$and++;
			}


			if ($this->nRating != '')
			{
				if ($and > 0)
				{
					$this->querySearch .= " AND ";
				}
				$this->querySearch .= '`rating`.`id`=' . $this->nRating;
				$and++;
			}
			
			if ($this->nOrder != '')
			{
				if ($and > 0)
				{
					$this->querySearch .= " AND ";
				}
				$this->querySearch .= '`rating`.`n_order`=' . $this->nOrder;
				$and++;
			}

			if ($this->nAkio != '')
			{
				if ($and > 0)
				{
					$this->querySearch .= " AND ";
				}
				$this->querySearch .= '`rating`.`n_akio`=' . $this->nAkio;
				$and++;
			}
			
			if ($this->intervalDate != '')
			{
				if ($and > 0)
				{
					$this->querySearch .= " AND ";
				}
				
				$date = explode('-', $this->intervalDate);
				if (isset($date[0]) && isset($date[1]))
				{
					$date[0] = str_replace('/', '-', $date[0]);
					$date[1] = str_replace('/', '-', $date[1]);
					
					$this->querySearch .= ' `rating`.`date_eval` BETWEEN "'.$date[0].'" AND "' . $date[1] .'"';

					$and++;
				}
			}

			if ($this->nameRater != '')
			{
				if ($and > 0)
				{
					$this->querySearch .= " AND ";
				}
				$this->querySearch .= '`rating`.`name_rater` LIKE "' . $this->nameRater .'"';
				$and++;	
			}

			if ($this->nameConsultant != '')
			{
				if ($and > 0)
				{
					$this->querySearch .= " AND ";
				}
				$this->querySearch .= '`rating`.`name_consultant` LIKE "' . $this->nameConsultant .'"';
				$and++;	
			}

			if (isset($this->minMark) && isset($this->maxMark))
			{
				if ($and > 0)
				{
					$this->querySearch .= " AND ";
				}
				$this->querySearch .= '`rating`.`final_mark` BETWEEN ' . $this->minMark .' AND ' . $this->maxMark;
				$and++;	
			}

			if ($this->improvement != '')
			{
				if ($and > 0)
				{
					$this->querySearch .= " AND ";
				}
				
				$this->querySearch .= '`rating`.`improvement` LIKE "' . $this->improvement .'"';
				$and++;	
			}

			if ($this->idTopic != '')
			{
				if ($and > 0)
				{
					$this->querySearch .= " AND ";
				}
				$this->querySearch .= '`topic`.`id` LIKE "' . $this->idTopic .'"';
				$and++;	
			}

			if ($this->idCountry != 0)
			{
				if ($and > 0)
				{
					$this->querySearch .= " AND ";
				}
				
				$this->querySearch .= '`country`.`id`=' . $this->idCountry;
				$and++;	
			}

			if ($this->idRecipient != 0)
			{
				if ($and > 0)
				{
					$this->querySearch .= " AND ";
				}
				
				$this->querySearch .= '`recipient`.`id`=' . $this->idRecipient;
				$and++;
			}			

			if (!empty($this->groups))
			{
				if ($and > 0)
				{
					$this->querySearch .= " AND ";
				}
				$group_string = implode(';', $this->groups);
				$this->querySearch .= '`rating`.`groups` LIKE "%' . $group_string . '%"';
				$and++;
			}

			if ($this->evalType != '')
			{
				if ($and > 0)
				{
					$this->querySearch .= " AND ";
				}
				$this->querySearch .= '`rating`.`eval_type` LIKE "' . $this->evalType . '"';
				$and++;
			}
			$this->querySearch .= ' GROUP BY `rating`.`id` ';
			return $this->querySearch;
		}

	}
?>